5-1 간단한 HTML 웹 페이지(간단한 XML 문서이기도 함)
<html>
<head>
<title>Introduction to the DOM</title>
</head>
<body>
<h1>Introduction to the DOM</h1>
<p class="test">There are number of reasons why the
DOM is awesome, here are some:</p>
<ul>
<li id="everywhere">It can be found everywhere.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
</ul>
</body>
</html>
5-2 XML 문서의 공백 버그를 피해가는 방법
function cleanWhitespace(element) {
//엘리먼트가 제공되지 않았다면 HTML 문서 전체에 함수를 적용한다.
element = element || document;
//첫째 자식을 시작점으로 삼는다.
var cur = element.firstChild;
//자식노드가 없을때까지 계속한다.
while (cur != null) {
//텍스트 노드이면서 공백밖에 없을 경우
if (cur.nodeType == 3 && ! /\S/.test(cur.nodeValue)) {
element.removeChild(cur);
//엘리먼트 일 경우
} else if (cur.nodeType == 1) {
cleanWhitespace(cur);
}
//다음 자식 노드로 이동
cur = cur.nextSibling;
}
}